
library(stargazer)
library(foreign)
library(stringr)
library(data.table)
library(gdata)
library(lfe)
library(dplyr)
library(cowplot)
library(DataCombine)
library(zoo)
rm(list=ls())



specify_decimal <- function(x, k) format(as.numeric(round(x, k), nsmall=k))
ihs <- function(x) log(x + sqrt(x^2+1))

mod_stargazer <- function(est) {
  capture.output(est)
}


### Load Regional Data
load(file="regionaldata.Rda")



#########################################################
#############     MAIN TEXT           ###################
#########################################################

###########################################
########     TABLE  3      ################
###########################################

est1_econ<-felm(natecon_spend ~ business_perc.lag  + total_spend_log | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est2_econ<-felm(natecon_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear + transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est3_edu<-felm(edu_spend ~ business_perc.lag  + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est4_edu<-felm(edu_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est5_health<-felm(health_spend ~ business_perc.lag  + total_spend_log | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est6_health<-felm(health_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear + transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est7_total<-felm(total_spend_log ~ business_perc.lag | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est8_total<-felm(total_spend_log ~ business_perc.lag + log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est9_deficit<-felm(deficit ~ business_perc.lag + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est10_deficit<-felm(deficit ~ business_perc.lag  + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)



#########################################################
#############     SUPPLEMENTARY APPENDIX           ###################
#########################################################

###########################################
########     TABLE   A3      ################
###########################################


regionaldata_summary<-regionaldata[,list(total_spend_log,
                               log_grp,
                               log_pop,
                               urbanization,
                               heldregionalelection,
                               transfers_depend,
                               gov_spend,
                               govprav_spend,
                               nondept_spend,
                               deficit,
                               health_spend,
                               edu_spend,
                               zkhx_spend,
                               soc_spend,
                               natecon_spend,
                               agr_spend,
                               fuel_spend,
                               roads_spend,
                               inv_private_log,
                               inv_regbudget_log,
                               unemployment)]

regionaldatatable<-mod_stargazer(stargazer(regionaldata_summary,covariate.labels=c("Total Budget Expenditures (log)","Gross Regional Product (log)","Population (log)","Urbanization","Region Election Year","Dependence on Subsidies (\\%)","Government Expenditures (\\%)","Legislative and Executive Branch Expenditures (\\%)","Other Agency Expenditures (\\%)","Deficit (\\%)","Health Expenditures (\\%)","Education Expenditures (\\%)","Housing Expenditures (\\%)","Social Policy Expenditures (\\%)","Total Economic Expenditures (\\%)","Agriculture Expenditures (\\%)","Fuel and Utilities Expenditures (\\%)","Roads and Transportation Expenditures (\\%)","Private Investment (log)","Regional Government Investment (log)","Unemployment (\\%)"),header=FALSE,digits=3))




###########################################
########     FIGURE   B3      ################
###########################################


exp1<-ggplot(data=regionaldata, aes(business_perc))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .05) + labs(x="Percentage Businesspeople in Legislature", y="Count")+xlim(0,1)+ theme(plot.title = element_text(size=12, face="plain"), axis.title = element_text(size=10),axis.text = element_text(size=10))

exp2<-ggplot(data=regionaldata, aes(total_spend_log))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .25) + labs(x="Total Expenditures (log)", y="Count")+xlim(22,29)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp3<-ggplot(data=regionaldata, aes(deficit))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Budget Deficit (%)", y="Count")+xlim(0.7,1.35)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp4<-ggplot(data=regionaldata, aes(gov_spend))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Government Expenditures (%)", y="Count")+xlim(0,.5)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp5<-ggplot(data=regionaldata, aes(natecon_spend))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Economic Expenditures (%)", y="Count")+xlim(0,.5)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp6<-ggplot(data=regionaldata, aes(health_spend))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Health Expenditures (%)", y="Count")+xlim(0,.5)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp7<-ggplot(data=regionaldata, aes(edu_spend))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Education Expenditures (%)", y="Count")+xlim(0,.5)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp8<-ggplot(data=regionaldata, aes(zkhx_spend))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Housing Expenditures (%)", y="Count")+xlim(0,.5)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp9<-ggplot(data=regionaldata, aes(all_social_spend))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Social Policy Expenditures (%)", y="Count")+xlim(0,.5)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))

exp10<-ggplot(data=regionaldata, aes(privateproperty_tax_rev))+geom_histogram(col="royalblue4",fill="royalblue4",alpha=.2, binwidth = .025) + labs(x="Revenue from Corporate Property Tax (%)", y="Count")+xlim(0,.5)+ theme(
  plot.title = element_text(size=12, face="plain"),
  axis.title = element_text(size=10),axis.text = element_text(size=10))


###########################################
########     TABLE   E7      ################
###########################################

est1_econ<-felm(natecon_spend ~ business_perc.lag  + total_spend_log | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est2_econ<-felm(natecon_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear + transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est3_econ<-felm(agr_spend ~ business_perc.lag  + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est4_econ<-felm(agr_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est5_econ<-felm(fuel_spend ~ business_perc.lag   + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est6_econ<-felm(fuel_spend ~ business_perc.lag  + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est7_econ<-felm(roads_spend ~ business_perc.lag + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est8_econ<-felm(roads_spend ~ business_perc.lag  + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est9_econ<-felm(privateproperty_tax_rev ~ business_perc.lag + total_income_log | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est10_econ<-felm(privateproperty_tax_rev ~ business_perc.lag  + total_income_log+ log_grp+ log_pop  + urbanization+ convocationyear + transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

###########################################
########     TABLE   E8      ################
###########################################


est1_health<-felm(health_spend ~ business_perc.lag  + total_spend_log | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est2_health<-felm(health_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear + transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est3_edu<-felm(edu_spend ~ business_perc.lag  + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est4_edu<-felm(edu_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est5_zhkx<-felm(zkhx_spend ~ business_perc.lag + total_spend_log| factor(regionid,psdef=FALSE) + factor(year) | 0 | regionid + year, data=regionaldata)

est6_zhkx<-felm(zkhx_spend ~ business_perc.lag + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est7_all<-felm(all_social_spend ~ business_perc.lag + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est8_all<-felm(all_social_spend ~ business_perc.lag  + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)


###########################################
########     TABLE   E9      ################
###########################################

est1<-felm(inv_private_log ~ business_perc.lag  + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est2<-felm(inv_private_log ~ business_perc.lag   + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est3<-felm(inv_regbudget_log ~ business_perc.lag  + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est4<-felm(inv_regbudget_log ~ business_perc.lag  + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est5<-felm(unemployment ~ business_perc.lag  + total_spend_log | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est6<-felm(unemployment ~ business_perc.lag  + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear + transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est9<-felm(tsennyibumagi_d_over_total ~ business_perc.lag + total_spend_log| factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)

est10<-felm(tsennyibumagi_d_over_total ~ business_perc.lag  + total_spend_log+ log_grp+ log_pop  + urbanization+ convocationyear+ transfers_depend.lag  + urmember + bus_plural + ur_perc | factor(regionid) + factor(year) | 0 | regionid + year, data=regionaldata,psdef=FALSE)